<?php

namespace  app\service;


use app\model\PartnerLogModel;
use app\model\PartnerRoleModel;
use app\model\PartnerUserModel;
use app\model\PermissionModel;
use app\model\RoleModel;
use app\utils\Md5Service;
use think\facade\Db;

Class PartnerRoleService{

    public  static  function  getAllRole($param)
    {
        return PartnerRoleModel::getAllRole($param);
    }

    public static  function getAllPartnerUser($param) :array
    {
        return PartnerUserModel::getPartnerUserByPartnerId($param);
    }

    public static  function add_role($param) :int
    {
        //查询mobile是否存在
        $where=[
            'name'=>$param['name']
        ];
        $partnerRoleInfo=PartnerRoleModel::find_role($where);
        if (!empty($partnerRoleInfo)){
            return  10001;
        }


        $data=[
            'name'=>$param['name'],
            'add_time'=>getNowDateTime(),
            'status'=>$param['status'],
            'permission_ids'=>$param['permission_ids'],
            'partner_id'=>$param['partner_id'],
            'pid'=>$param['pid'],

        ];
        Db::startTrans();
        $addPartnerId=PartnerRoleModel::add_role($data);
        $dataLog=[
            'partner_user_id'=>$param['uid'],
            'log_type'=>2,
            'request_ip'=>$param['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'新增合作商角色',
            'status'=>1,
            'partner_id'=>$param['partner_id']
        ];
        
         if ($addPartnerId){
             Db::commit();
             return 10000;
         }else{
             // 回滚事务
             Db::rollback();
             return 10003;
         }
    }

    public static  function modify_role($param,$role_id) :int
    {

        $where=[
            'id'=>$role_id
        ];
        $partnerRoleInfo=PartnerRoleModel::find_role($where);
        if (empty($partnerRoleInfo)){
            return  10001;
        }
        $data=[
            'name'=>$param['name'],
            'add_time'=>getNowDateTime(),
            'status'=>$param['status'],
            'permission_ids'=>$param['permission_ids'],
            'partner_id'=>$param['partner_id'],
            'pid'=>$param['pid'],

        ];
        Db::startTrans();
        $addPartnerId=PartnerRoleModel::modify_role($data,$role_id);
        $dataLog=[
            'partner_user_id'=>$param['uid'],
            'log_type'=>2,
            'request_ip'=>$param['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'修改合作商角色',
            'status'=>1,
            'partner_id'=>$param['partner_id']
        ];
        
        if ($addPartnerId){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }

    public static  function del_role($param,$role_id) :int
    {

        $where=[
            'id'=>$role_id,
            'status'=>1
        ];
        $partnerRoleInfo=PartnerRoleModel::find_role($where);
        if (empty($partnerRoleInfo)){
            return  10001;
        }

        Db::startTrans();
        $status=[

            'status'=>3
        ];
        $addPartnerId=PartnerRoleModel::modify_role($status,$role_id);
        $dataLog=[
            'partner_user_id'=>$param['uid'],
            'log_type'=>2,
            'request_ip'=>$param['ip'],
            'add_time'=>getNowDateTime(),
            'log_desc'=>'删除合作商角色',
            'status'=>1,
            'partner_id'=>$param['partner_id']
        ];
        
        if ($addPartnerId){
            Db::commit();
            return 10000;
        }else{
            // 回滚事务
            Db::rollback();
            return 10003;
        }
    }



}